package com.superhelper.superhelper_web.controller;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fasterxml.jackson.core.type.TypeReference;
import com.superhelper.common.domain.AuserDeliver;
import com.superhelper.common.domain.AuserShop;
import com.superhelper.common.domain.TakeOutShopInfo;
import com.superhelper.common.utils.JsonHelper;
import com.superhelper.superhelper_web.common.CommonHelper;
import com.superhelper.superhelper_web.common.Consts;
import com.superhelper.superhelper_web.controller.base.AbstractController;
import com.superhelper.superhelper_web.domain.Admin;
import com.superhelper.superhelper_web.domain.AuserShopAudio;
import com.superhelper.superhelper_web.domain.PageModel;
import com.superhelper.superhelper_web.domain.Resp;
import com.superhelper.superhelper_web.service.DeliverService;
import com.superhelper.superhelper_web.service.MtService;
import com.superhelper.superhelper_web.service.ShopService;

import eleme.openapi.sdk.api.entity.shop.OShop;
import eleme.openapi.sdk.api.exception.ServiceException;

@Controller
@RequestMapping("admin/shop")
public class ShopController extends AbstractController {

	@Autowired
	private ShopService shopService;
	@Autowired
	private MtService mtService;
	@Autowired
	private DeliverService deliverService;

	/**
	 * 查看
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("list")
	public String list(ModelMap model, HttpServletRequest request,
			PageModel pageModel) throws Exception {
		Admin admin = CommonHelper.getSessionUser();

		List<AuserShop> initShops = shopService.getInitShop();
		List<AuserShop> list = shopService.selectList(admin.getId());
		if (list.isEmpty()) {
			list.addAll(initShops);
		} else {
			Map<String, AuserShop> map = new HashMap<String, AuserShop>();
			for (AuserShop s : list) {
				map.put(s.getShop_type(), s);
			}
			for (AuserShop ishop : initShops) {
				if (!map.containsKey(ishop.getShop_type())) {
					list.add(ishop);
				}
			}
		}
		for (AuserShop s : list) {
			if (s.getShop_type().equals(AuserShop.SHOP_ELEME)) {
				s.setLogo("eleme.png");
				if (s.getShopId() != null && !s.getShopId().equals("0")) {
					TakeOutShopInfo shop = shopService.getShopInfo(
							s.getShopId(), AuserShop.SHOP_ELEME);
					s.setShop(shop);
				}
			} else if (s.getShop_type().equals(AuserShop.SHOP_MEITUAN)) {
				if (s.getShopId() != null && !s.getShopId().equals("0")) {
					TakeOutShopInfo shop = shopService.getShopInfo(
							s.getShopId(), AuserShop.SHOP_MEITUAN);
					s.setShop(shop);
				}
				s.setLogo("meituan.png");
			}
		}
		model.put("list", list);
		return "admin/shop/list";
	}

	/**
	 * 跳转授权页面
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("auth")
	@ResponseBody
	public Resp auth(ModelMap model, @RequestParam String shop_type,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		Admin user = CommonHelper.getSessionUser();
		String authUrl = shopService.getAuthUrl(user.getId(), shop_type);
		return success(authUrl);
	}

	/**
	 * 自动接单设置
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("order/switch")
	@ResponseBody
	public Resp orderSwitch(@RequestParam Integer orderAuto,
			@RequestParam Integer id, HttpServletResponse response)
			throws IOException {
		int res = shopService.updateOrderSwitch(id, orderAuto);
		return res > 0 ? success("") : fail("设置失败");
	}

	/**
	 * 配送设置
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("deliver/switch")
	@ResponseBody
	public Resp deliverSwitch(@RequestParam Integer deliverType,
			String deliverUid, String deliverShopNo, @RequestParam Integer id,
			HttpServletResponse response) throws IOException {
		int res = shopService.updateDeliverSwitch(id, deliverType, deliverUid,
				deliverShopNo);
		return res > 0 ? success("") : fail("设置失败");
	}

	/**
	 * 设置页面
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("setting")
	public String setting(ModelMap model) {
		Admin user = CommonHelper.getSessionUser();
		List<AuserShopAudio> list = shopService.getAuserAudioByAuserId(user
				.getId());
		model.addAttribute("list", list);
		return "admin/shop/setting";
	}

	/**
	 * 提示语音设置
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("audio/save")
	@ResponseBody
	public Resp audioSave(@RequestParam String content,
			HttpServletResponse response) {
		if (StringUtils.isNoneBlank(content)) {
			List<AuserShopAudio> list = JsonHelper.fromJson(content,
					new TypeReference<List<AuserShopAudio>>() {
					});
			Admin user = CommonHelper.getSessionUser();
			for (AuserShopAudio a : list) {
				a.setAuserId(user.getId());
			}
			int res = shopService.saveAudio(list);
			if (res > 0) {
				return success("");
			}
		}
		return fail("设置失败");
	}

	/**
	 * 设置店铺状态
	 * 
	 * @return
	 * @throws Exception
	 * @throws IOException
	 */
	@RequestMapping("open")
	@ResponseBody
	public Resp open(@RequestParam String shopId, @RequestParam String type,
			@RequestParam Integer state, HttpServletResponse response)
			throws Exception {
		boolean f = shopService.openShop(shopId, type, state);
		return f ? success("") : fail("");
	}

	/**
	 * 首页店铺信息
	 * 
	 * @return
	 * @throws Exception
	 * @throws IOException
	 */
	@RequestMapping("info")
	@ResponseBody
	public Resp shopInfo() throws Exception {
		Admin admin = CommonHelper.getSessionUser();
		List<AuserShop> list = shopService.selectList(admin.getId());
		AuserDeliver deliverConf = deliverService
				.getAuserDeliver(admin.getId());
		List<AuserShop> result = new ArrayList<AuserShop>();
		for (AuserShop s : list) {
			try {
				if (s.getShop_type().equals(AuserShop.SHOP_ELEME)) {
					s.setLogo("eleme.png");
					if (s.getShopId() != null && !s.getShopId().equals("0")) {
						TakeOutShopInfo shop = shopService.getShopInfo(
								s.getShopId(), AuserShop.SHOP_ELEME);
						s.setShop(shop);
					}
					s.setDeliverConf(deliverConf);
				} else if (s.getShop_type().equals(AuserShop.SHOP_MEITUAN)) {
					if (s.getShopId() != null && !s.getShopId().equals("0")) {
						TakeOutShopInfo shop = shopService.getShopInfo(
								s.getShopId(), AuserShop.SHOP_MEITUAN);
						s.setShop(shop);
					}
					s.setLogo("meituan.png");
					s.setDeliverConf(deliverConf);
				}
				result.add(s);
			} catch (Exception e) {
				log.error("", e);
			}
		}
		return success(result);
	}

}
